#include "musicslider.h"
#include "ui_musicslider.h"

#include <QMouseEvent>
#include <QDebug>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    maxWidth = width();

    currentPos = 0;
    moveSlider();
}


MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    //进行拖拽的时候，event->pos()需要在MusicSlider窗口内部，才能支持拖拽的功能
    //检测: event->pos()是否在MusicSlier的窗口矩形范围内
    QRect musicSliderRect = QRect(0, 0, geometry().width(), geometry().height()); //获得的geometry()是相当于父窗口的
    if(musicSliderRect.contains(event->pos()))
    {
       // qDebug() << event->pos();
        //要判断鼠标产生的坐标是不是在这个矩形内部
        if(event->buttons() == Qt::LeftButton)
        {
            currentPos = event->pos().x();

            if(currentPos < 0)
            {
                currentPos = 0;
            }

            if(currentPos > maxWidth)
            {
                currentPos = maxWidth;
            }
            moveSlider();
        }
    }
}


void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();
}



void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();

    emit setMusicSliderPostion(ui->outline->width() / (float)maxWidth);
}


void MusicSlider::moveSlider()
{
   // qDebug() << currentPos;
    ui->outline->setGeometry(ui->outline->x(), ui->outline->y(), currentPos, ui->outline->height());
}


void MusicSlider::setStep(float ratio)
{
    currentPos = maxWidth * ratio;
    moveSlider();
}




//MusicSlider需要处理 鼠标按下，鼠标移动，鼠标抬起事件
//更新outLine的宽度，而outLine的宽度实际就是鼠标点击时pos的x坐标
//注意:鼠标点击pos一般是相对于screen(屏幕) ,在使用pos的时候需要将其转换为musicSlider的相对坐标
//

